﻿function CValidator(opts) {
    var self = this;
    this.container = "";
    this.validator = {};
    this.options = {
        container: "form",
        grouped: false, 		// show all error messages at once inside the container 
        effect: 'default', 		// show/hide effect for error message. only 'default' is built-in
        errorClass: 'invalid', 	// input field class name in case of validation error		
        // when to check for validity?
        inputEvent: 'blur', 			// change, blur, keyup, null 
        errorInputEvent: 'keyup',   // change, blur, keyup, null
        formEvent: 'submit',        // submit, null
        lang: 'en-US', 			    // default language for error messages 
        message: '<div><em/></div>', // em element is the arrow
        messageAttr: 'data-message', // name of the attribute for overridden error message
        messageClass: '', 	    // error message element's class name
        offset: [-5, 0],
        position: 'top left',
        singleError: true, 		// validate all inputs at once
        speed: 'normal', 			// message's fade-in speed		
        zIndex: 9999,
        onFail: function (e, els) {
            //if (els && els[0] && els[0].input && els[0].input[0]) {
            //    var id = els[0].input[0].id;
            //    $("#" + id).focus();
            //}
        },
        onBeforeFail: function (e, els) { },
        onBeforeValidate: function (e, els) { },
        onSuccess: function (e, els) { }
    };
    $.extend(true, self.options, opts);
    var fn_CustomValidators = function () {
        $.tools.validator.fn(
            "[validator-range]",
            function (input, value) {
                // get min and max for this field from the (HTML5) attributes
                var min = input.attr("range").split("-")[0];
                var max = input.attr("range").split("-")[1];
                value = parseFloat(value);
                // if the value is inside the range return true otherwise
                // return values for replacement
                return value >= min && value <= max ? true : [min, max];
            }
        );
        $.tools.validator.fn(
            "[validator-evals]",
            function (input, value) {
                // get min and max for this field from the (HTML5) attributes
                var val = input.attr("validator-evals");
                value = parseFloat(value);
                // if the value is inside the range return true otherwise
                // return values for replacement
                return value == val ? true : [val];
            }
        );
        $.tools.validator.fn(
            "[validator-compare]",
            function (input, value) {
                // get min and max for this field from the (HTML5) attributes
                var val = input.attr("validator-compare");
                value = parseFloat(value);
                // if the value is inside the range return true otherwise
                // return values for replacement
                return value == val ? true : [val];
            }
        );
    };
    var fn_CustomLocalize = function () {
        var culture = "en";
        var validate_caption = {
            'email': 'Please enter a valid email address.',
            'number': 'Please enter a numeric value.',
            'url': 'Please enter a valid URL.',
            'max': 'Please enter a value larger than $1',
            'min': 'Please enter a value smaller than $1',
            'required': 'Vui lòng nhập thông tin.',
            'range': 'Value must be between $1 and $2',
            'validator-evals': 'Value must be eval $1'
        };
        var localize = {};
        $.each(validate_caption, function (key, value) {
            localize["[" + key.toLowerCase() + "]"] = value;
        });
        $.tools.validator.localize(culture, localize);
        self.options.lang = culture;
    };
    this.Init = function (container) {
        self.container = container;
        fn_CustomValidators();
        fn_CustomLocalize();
        if (self.options.messageClass) {
            self.options.messageClass = "CValidator-Error " + self.options.messageClass;
        }
        else {
            self.options.messageClass = "CValidator-Error";
        }
        self.validator = $(container).validator(self.options);
    };
    this.CheckValidity = function () {
        return self.validator.data("validator").checkValidity();
    };
    this.GetConf = function () {
        return self.validator.data("validator").getConf();
    };
    this.GetForm = function () {
        return self.validator.data("validator").getForm();
    };
    this.GetInputs = function () {
        return self.validator.data("validator").getInputs();
    };
    this.Clear = function () {
        $(".CValidator-Error").hide();
    };
    this.Destroy = function () {
        self.validator.data("validator").destroy();
    };
}